package lzk.myshop.dao;


import lzk.myshop.pojo.Users;
import org.apache.ibatis.annotations.*;

@Mapper
public interface UsersMapper{
    /**
     * 手机号登录
     * */
    @Select("select* from users where `phone`=#{phone} and `password`=#{password} and `status`=1 ")
    Users selectbyphone(String phone, String password);


    /**
     * 邮箱登录
     * */
    @Select("select* from users where `email`= #{email} and `password`= #{password} and `status`= 1")
    Users selectbyemail(String email, String password);


    /**
     * 通过手机号和密码获取用户id
     * */
    @Select("select `id` from users where `phone`=#{phone} and `password`=#{password} and `status`=1 ")
    int getId(String phone, String password);



    /**
     * 判断账户（手机号或邮箱）是否已经存在
     * */
    @Select("SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE `email` = #{email} OR `phone` = #{phone}) THEN '存在' ELSE '不存在' END AS result")
    String ifExist(String email,String phone);



    /**
     * 用户注册
     * */
    @Insert("INSERT INTO users (id, name, email, phone, password, address) VALUES (#{id}, #{name}, #{email}, #{phone}, #{password}, #{address})")
    int save(Users users);


    /**
     * 修改手机号
     * */
    @Update("UPDATE users SET `phone` = #{phone} WHERE `id`= #{id}")
    int alterPhone(String phone,int id);
    /**
     * 判断手机号是否已经存在
     * */
    @Select("SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE `phone` = #{phone}) THEN '存在' ELSE '不存在' END AS result")
    String ifPhoneExist(String phone);



    /**
     * 修改邮箱
     * */
    @Update("UPDATE users SET `email` = #{email} WHERE `id`= #{id}")
    int alterEmail(String email,int id);
    /**
     * 判断邮箱是否已经存在
     * */
    @Select("SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE `email` = #{email}) THEN '存在' ELSE '不存在' END AS result")
    String ifEmailExist(String email);



    /**
     * 修改地址
     * */
    @Update("UPDATE users SET `address` = #{address} WHERE `id`= #{id}")
    int alterAddress(String address,int id);

    @Update("update `users` set `status` = 0 where `userid` = #{id}")
    int deleteUser(int id);

    @Select("SELECT COUNT(*) FROM `user` WHERE `id` = #{userid} AND `status` NOT IN (3, -1)")
    int ifCanDelete(int userid);

    @Select("select * from `users` where `id` = #{id}")
    Users getUserByID(int id);
}
